package com.fyw.xmcamera;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;

import com.lib.EPTZCMD;
import com.lib.funsdk.support.FunSupport;
import com.lib.funsdk.support.models.FunDevice;
import com.lib.funsdk.support.models.FunStreamType;
import com.lib.funsdk.support.widget.FunVideoView;

/**
 * Created by FYW on 2017/10/18.
 */

public class PlayActivity extends BaseActivity implements View.OnTouchListener {
    private FunVideoView mFunVideoView;

    private FunDevice mFunDevice = null;

    private ImageButton ptz_top_btn;//上
    private ImageButton ptz_left_btn;//左
    private ImageButton ptz_bottom_btn;//右
    private ImageButton ptz_right_btn;//下

    @Override
    protected void handleToolbar(ToolbarHelper toolbarHelper) {
        toolbarHelper.setTitle("实时视频");
        toolbarHelper.setFinish().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void initParam(Bundle param) {
        int devId = getIntent().getIntExtra("FUN_DEVICE_ID", 0);
        mFunDevice = FunSupport.getInstance().findDeviceById(devId);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_play;
    }

    @Override
    public void initView(View view) {
        mFunVideoView = view.findViewById(R.id.funVideoView);

        ptz_top_btn = view.findViewById(R.id.ptz_top_btn);
        ptz_left_btn = view.findViewById(R.id.ptz_left_btn);
        ptz_bottom_btn = view.findViewById(R.id.ptz_bottom_btn);
        ptz_right_btn = view.findViewById(R.id.ptz_right_btn);

        ptz_top_btn.setOnTouchListener(this);
        ptz_left_btn.setOnTouchListener(this);
        ptz_bottom_btn.setOnTouchListener(this);
        ptz_right_btn.setOnTouchListener(this);

    }

    @Override
    public void doBusiness(Context mContext) {
        playRealMedia();
    }

    private void playRealMedia() {

        // 显示状态: 正在打开视频...
       /* mTextVideoStat.setText(R.string.media_player_opening);
        mTextVideoStat.setVisibility(View.VISIBLE);*/

        if (mFunDevice.isRemote) {
            mFunVideoView.setRealDevice(mFunDevice.getDevSn(), mFunDevice.CurrChannel);
        } else {
            String deviceIp = FunSupport.getInstance().getDeviceWifiManager().getGatewayIp();
            mFunVideoView.setRealDevice(deviceIp, mFunDevice.CurrChannel);
        }

        // 打开声音
        mFunVideoView.setMediaSound(true);

        // 设置当前播放的码流类型
        if (FunStreamType.STREAM_SECONDARY == mFunVideoView.getStreamType()) {
            //mTextStreamType.setText(R.string.media_stream_secondary);
        } else {
            //mTextStreamType.setText(R.string.media_stream_main);
        }
    }

    boolean bStop = true;
    int nPTZCommand = -1;

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                switch (view.getId()) {
                    case R.id.ptz_flip_btn:
                        break;
                    case R.id.ptz_top_btn://上
                        bStop = false;
                        nPTZCommand = EPTZCMD.TILT_DOWN;
                        break;
                    case R.id.ptz_left_btn://左
                        bStop = false;
                        nPTZCommand = EPTZCMD.PAN_LEFT;
                        break;
                    case R.id.ptz_bottom_btn://下
                        bStop = false;
                        nPTZCommand = EPTZCMD.TILT_UP;
                        break;
                    case R.id.ptz_right_btn://右
                        bStop = false;
                        nPTZCommand = EPTZCMD.PAN_RIGHT;
                        break;
                    default:
                        break;
                }
                break;
            case MotionEvent.ACTION_UP:
                switch (view.getId()) {
                    case R.id.ptz_flip_btn:
                        break;
                    case R.id.ptz_top_btn:
                        bStop = true;
                        nPTZCommand = EPTZCMD.TILT_DOWN;
                        break;
                    case R.id.ptz_left_btn:
                        bStop = true;
                        nPTZCommand = EPTZCMD.PAN_LEFT;
                        break;
                    case R.id.ptz_bottom_btn:
                        bStop = true;
                        nPTZCommand = EPTZCMD.TILT_UP;
                        break;
                    case R.id.ptz_right_btn:
                        bStop = true;
                        nPTZCommand = EPTZCMD.PAN_RIGHT;
                        break;
                    default:
                        break;
                }
                break;
            case KeyEvent.ACTION_MULTIPLE:
                switch (view.getId()) {
                    case R.id.ptz_flip_btn:
                        break;
                    case R.id.ptz_top_btn:
                        nPTZCommand = EPTZCMD.TILT_DOWN;
                        bStop = Math.abs(motionEvent.getX()) > view.getWidth()
                                || Math.abs(motionEvent.getY()) > view.getHeight();
                        break;
                    case R.id.ptz_left_btn:
                        nPTZCommand = EPTZCMD.PAN_LEFT;
                        bStop = Math.abs(motionEvent.getX()) > view.getWidth()
                                || Math.abs(motionEvent.getY()) > view.getHeight();
                        break;
                    case R.id.ptz_bottom_btn:
                        nPTZCommand = EPTZCMD.TILT_UP;
                        bStop = Math.abs(motionEvent.getX()) > view.getWidth()
                                || Math.abs(motionEvent.getY()) > view.getHeight();
                        break;
                    case R.id.ptz_right_btn:
                        nPTZCommand = EPTZCMD.PAN_RIGHT;
                        bStop = Math.abs(motionEvent.getX()) > view.getWidth()
                                || Math.abs(motionEvent.getY()) > view.getHeight();
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
        onControl(nPTZCommand, bStop);
        return false;
    }

    private void onControl(int nPTZCommand, boolean bStop) {
        FunSupport.getInstance().requestDevicePTZControl(mFunDevice,
                nPTZCommand, bStop, mFunDevice.CurrChannel);
    }
}
